<?php

namespace components\frame;

use \components\orm\dbclass as dbclass;

class model {

    private $orm;
    private $config;
    protected $model = "";
    protected $table = "";
    private $record;
    public $verror = false;
    public $notice = array();
    private $query = array();
    private $field = array();
    private $limit = array();
    private $order = array();
    protected $rules = array();

    public function __construct($config) {
        $this->config = $config;
        $this->orm = new \components\orm\orm($config);
        $this->orm->getConn();
        $this->orm->setTable($this->getTable());
    }

    private function getTable() {
        $config = $this->config;
        $class = strtolower(get_called_class());
        $model = explode("\\", $class);
        $table = $model[count($model) - 1];
        if ($this->model == "" && $this->table == "") {
            if (isset($config['prefix'])) {
                $table = $config['prefix'] . $table;
            }
            if (isset($config['suffix'])) {
                $table .= $config['suffix'];
            }
            return $table;
        } else if ($this->model != "" && $this->table == "") {
            if (isset($config['prefix'])) {
                $table = $config['prefix'] . $table;
            }
            return $table;
        } else {
            return $table;
        }
    }

    public function create() {
        if ($this->doVerify()) {
            $this->orm->create($this->record);
        } else {
            return false;
        }
        return $this;
    }

    public function save() {
        if (!empty($this->query)) {
            $this->orm->query($this->query)->update();
        } else {
            $this->orm->add();
        }
        $this->query = array();
        $this->record = array();
        return $this;
    }

    public function findAll() {
        $arr = $this->orm->field($this->field)->query($this->query)->findAll();
        $this->query = array();
        $this->field = array();
        return $arr;
    }

    public function delete() {
        $this->orm->query($this->query)->delete();
        $this->query = array();
        return $this;
    }

    public function setRecord($record = array()) {
        $this->record = $record;
        return $this;
    }

    public function setQuery($query = array()) {
        $this->query = $query;
        return $this;
    }

    public function setField($field = array()) {
        $this->field = $field;
        return $this;
    }
    public function setLimit($start,$stop){
        $this->limit = array($start,$stop);
        return $this;
    }
    public function setOrder($field,$type = ""){
        $this->order = array($field, $type);
        return $this;
    }

    private function doVerify() {
        if (is_file(PRO_PATH . '/expand/verify/verify.class.php')) {
            require_once PRO_PATH . '/expand/verify/verifies.class.php';
            if (class_exists(\expend\verify\verifies)) {
                $verify = new \expend\verify\verifies();
            } else {
                $verify = new \components\frame\model\verify();
            }
        } else {
            $verify = new \components\frame\model\verify();
        }
        foreach ($this->rules as $key => $value) {
            if (isset($this->record[$key])) {
                $column = $this->record[$key];
                if (is_array($value)) {
                    foreach ($value as $k => $v) {
                        $ver = "ver" . ucwords($k);
                        if (!$verify->$ver($column, $v)) {
                            $this->verror = isset($this->notice[$key][$k]) ? $this->notice[$key][$k] : "error";
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }

    public function __call($fun, $arg) {
        if (substr_count($fun, "set") == 1) {
            $fun = explode("set", $fun);
            $field = $fun[1];
            $this->record[$field] = $arg[0];
            return $this;
        } elseif (substr_count($fun, "get") == 1) {
            $fun = explode("get", $fun);
            $field = $fun[1];
            $this->field[] = $field;
            return $this;
        }
    }

    public function __set($name, $value) {
        $this->$name = $value;
    }

    public function __get($name) {
        return $this->$name;
    }

}

?>
